<?php
class Admin_ChucVuController extends Zend_Controller_Action
{

    public function init()
    {
          $this->_helper->layout->setLayout('admin');
    }

    public function indexAction()
    {
    	$chucvuMapper = Library_Model_ChucVuMapper::getInstance();
    	$chucvu = $chucvuMapper->chucvuList();
    	$this->view->chucvu = $chucvu;
    }

    public function themAction()
    {
    	$this->view->title = 'Thêm chức vụ';
    	$formThem = new Admin_Form_ChucVuForm();
    	$formThem->setMethod('post');
    	$formThem->setAction('/zfMHB/public/admin/chucvu/them');
    	
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	$formThem->getElement('tenchucvu')->addValidator('Db_NoRecordExists',true, array(
    			'table' => 'tbl_chucvu',
    			'field' => 'TenChucVu'
    	));
    	$this->view->form = $formThem;
    	
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			
    			$tenchucvu 		= $data['tenchucvu'];    					
    			$chucvuMapper = Library_Model_ChucVuMapper::getInstance();
    			$chucvuObj 	= new Library_Model_ChucVu();
    			    			
    			$chucvuObj->setTenchucvu($tenchucvu);    			
    	
    			$chucvu = $chucvuMapper->save($chucvuObj);
    			$this->_redirect('/admin/chucvu/index');
    		}
    	}
    }

    public function capnhatAction()
    {
        $this->view->title = 'Cập nhật chức vụ';
        $formCapnhat = new Admin_Form_ChucVuForm();        
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/chucvu/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
      	$machucvu = $this->getRequest()->getParam('id');
        
        $formCapnhat->getElement('tenchucvu')->addValidator('Db_NoRecordExists',true, array(
        		'table' => 'tbl_chucvu',
        		'field' => 'TenChucVu',
        		'exclude'	=> array(
        				'field'=> 'MaChucVu',
        				'value' => $machucvu) 
        ));
        
        $formCapnhat->getElement('tenchucvu')->getValidator('Db_NoRecordExists')->setMessage('Chức vụ đã tồn tại!');
        $chucvuMapper = Library_Model_ChucVuMapper::getInstance();
        $this->view->form = $formCapnhat;
        
        if($this->getRequest()->isPost()){
        	if($formCapnhat->isValid($_POST)){
        		$data = $formCapnhat->getValues();
        		
        		$id 				=$data['id'];
        		$tenchucvu 		= $data['tenchucvu'];
        		
			
    			$chucvuObj 	= new Library_Model_ChucVu();

    			$chucvuObj->setId($id);
    			$chucvuObj->setTenchucvu($tenchucvu);
    			
    			$chucvu = $chucvuMapper->save($chucvuObj);
    			$this->_redirect('/admin/chucvu/index');
        	}
        } else {        	
        		$chucvu = $chucvuMapper->searchChucVuById($machucvu);
        		
        		$formCapnhat->getElement('id')->setValue($chucvu->getId());
        		$formCapnhat->getElement('tenchucvu')->setValue($chucvu->getTenchucvu());        		
			}
        
    }

    public function xoaAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        $machucvu = $this->getRequest()->getParam('id');
        $chucvuMapper = Library_Model_ChucVuMapper::getInstance();
        $chucvuMapper->delete($machucvu);
        $this->_redirect('/admin/chucvu/index');
    }
    
}









		
		
		
		
		
		
		